
WorkerScript.onMessage = function(message) {
    doSponsorConnect(message)
}

function doSponsorConnect(message) {
    var xhr = new XMLHttpRequest();
    var url = "http://coscup.org/2011/api/sponsors/"

    message.model.clear();
    WorkerScript.sendMessage({"result": "PROGRESS"});

    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        try {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                var response = JSON.parse(xhr.responseText);
                for (var i in response) {
                    var jsonData = response[i];
                    for (var j in jsonData) {
                        message.model.append({
                                             level: i,
                                             desc: jsonData[j].desc["zh-tw"],
                                             logoUrl: jsonData[j].logoUrl,
                                             name: jsonData[j].name["zh-tw"],
                                             url: jsonData[j].url
                        })
                    }

                }
            }
            message.model.sync();
            WorkerScript.sendMessage({"result": "SUCCESS"});
        } catch (err) {
            WorkerScript.sendMessage({"result": "FAILURE"});
            //ERROR
        }
    }
    xhr.send();
}
